﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Glossary
{
    /// <summary>
    /// Ansvar: Vikiet Chung
    /// </summary>
    public class WordLibrary
    {
        private List<Word> _englishLibrary = new List<Word>();
        private List<Word> _germanLibrary = new List<Word>();
        private List<Word> _spanishLibrary = new List<Word>();
        private Random rnd = new Random();

        public List<Word> EnglishLibrary
        {
            get { return _englishLibrary; }
            private set { _englishLibrary = value; }
        }
        public List<Word> GermanLibrary
        {
            get { return _germanLibrary; }
            set { _germanLibrary = value; }
        }
        public List<Word> SpanishLibrary
        {
            get { return _spanishLibrary; }
            private set { _spanishLibrary = value; }
        }

        public void AddWord(Word word)
        {
            switch (word.ForeignLanguage)
            {
                case Language.English:
                    if (!EnglishLibrary.Contains(word))
                    {
                        EnglishLibrary.Add(word);
                    }
                    break;
                case Language.German:
                    if (!GermanLibrary.Contains(word))
                    {
                        GermanLibrary.Add(word);
                    }
                    break;
                case Language.Spanish:
                    if(!SpanishLibrary.Contains(word))
                    {
                        SpanishLibrary.Add(word);
                    }
                    break;
                default:
                    throw new Exceptions.UnknownLanguageException();
            }
        }

        /// <summary>
        /// Ansvar: Martin & Kit
        /// Denna metod hämtar antal valda glosor från valt språk och gör random på dem.
        /// </summary>
        /// <param name="language"></param>
        /// <param name="numberOfQuestions"></param>
        /// <param name="rnd"></param>
        /// <returns>result</returns>
        public Word[] GetRandomWords(Language language, int numberOfQuestions)
        {
            switch (language)
            {
                case Language.English:
                    return GetRandomWords(EnglishLibrary, numberOfQuestions);
                case Language.German:
                    return GetRandomWords(GermanLibrary, numberOfQuestions);
                case Language.Spanish:
                    return GetRandomWords(SpanishLibrary, numberOfQuestions);
                default:
                    throw new Exceptions.UnknownLanguageException();
            }
        }

        private Word[] GetRandomWords(List<Word> library, int numberOfQuestions)
        {
            Word[] result = new Word[numberOfQuestions];
            int[] indexes = Enumerable.Range(0, library.Count).OrderBy(x => rnd.Next()).ToArray();
            for (int i = 0; i < numberOfQuestions; i++)
            {
                if (library.Count < numberOfQuestions)
                    throw new ArgumentOutOfRangeException("Inte tillräckligt med ord för att skapa prov!");
                result[i] = library[indexes[i]];
            }
            return result;
        }

        /// <summary>
        /// Ansvar: Kim Nielsen
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        public List<Word> GetWordLibrary(Language language)
        {
            switch (language)
            {
                case Language.English:
                    return EnglishLibrary;
                case Language.German:
                    return GermanLibrary;
                case Language.Spanish:
                    return SpanishLibrary;
                default:
                    throw new Exceptions.UnknownLanguageException();
            }
        }

        public string GetForeignWord(Language language, String swedishWord)
        {
            switch (language)
            {
                case Language.English:

                    String[] strArr =  EnglishLibrary
                        .Where(x => string.Compare(x.SwedishWord, swedishWord, false) == 0)
                        .Select(x => x.ForeignWord).ToArray();
                    if (strArr.Length == 1)
                    {
                        return strArr[0];
                    }
                    break;
                case Language.German:
                    foreach (Word word in GermanLibrary)
                    {
                        if (string.Compare(word.SwedishWord, swedishWord, false) == 0)
                        {
                            return word.ForeignWord;
                        }
                    }
                    break;
                case Language.Spanish:
                    foreach (Word word in SpanishLibrary)
                    {
                        if (string.Compare(word.SwedishWord, swedishWord, false) == 0)
                        {
                            return word.ForeignWord;
                        }
                    }
                    break;
                default:
                    throw new Exceptions.UnknownLanguageException();
            }
            throw new Exceptions.WordNotInLibraryException("Ordet fanns inte i Biblioteket.");
        }
    }
}
